package net.anzhuo.controller.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.CompanyNameUtils;
import net.anzhuo.common.utils.DateUtil;

import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsBranch;

import net.anzhuo.entity.GsEnquityPenetration;
import net.anzhuo.entity.GsGudong;
import net.anzhuo.entity.GsJudgeDocCollect;
import net.anzhuo.entity.GsOuttouzi;

import net.anzhuo.entity.GsQuery;

import net.anzhuo.entity.db.spiderman.DetailList;
import net.anzhuo.entity.db.spiderman.Gudong;
import net.anzhuo.entity.db.spiderman.JsonRootBean;
import net.anzhuo.entity.db.spiderman.Kg;
import net.anzhuo.entity.db.spiderman.Kzr;
import net.anzhuo.entity.db.spiderman.SyrNames;
import net.anzhuo.entity.db.spiderman.Oper;
import net.anzhuo.entity.db.spiderman.Syr;
import net.anzhuo.entity.db.spiderman.Touzi;
import net.anzhuo.entity.db.syncdata.PairValue;
import net.anzhuo.entity.db.syncdata.QueryKeyNoResult;
import net.anzhuo.entity.db.syncdata.UpdateKeyNoResult;
import net.anzhuo.entity.equitypenetration.EnquityPenetrationResult;
import net.anzhuo.entity.equitypenetration.EnquityResult;
import net.anzhuo.entity.equitypenetration.KeyNoListResult;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsBranchService;

import net.anzhuo.service.GsEnquityPenetrationService;
import net.anzhuo.service.GsGudongService;
import net.anzhuo.service.GsJudgeDocCollectService;
import net.anzhuo.service.GsOuttouziService;

import net.anzhuo.service.GsQueryService;

@RestController
@Slf4j
public class JudgeDocCollectController {

	
	
	@Autowired
	private TransportClient client;
	
	
	@Autowired
	private GsJudgeDocCollectService gsJudgeDocCollectService;
	
	@Autowired
	private GsBaseService gsBaseService;
	
	
	
	@RequestMapping("/JudgeDocCollectKeyNoList")
	public QueryKeyNoResult JudgeDocCollectKeyNoList(String status) {
		log.info("===>JudgeDocCollectKeyNoList="+status);
		long startTime=System.currentTimeMillis();
		QueryKeyNoResult qknr=new QueryKeyNoResult();		
		try {	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsJudgeDocCollect gp=new GsJudgeDocCollect();
			gp.setStatus(status);
		   List<GsJudgeDocCollect> list=gsJudgeDocCollectService.getList(gp);
		  
		   List<PairValue> keyNoList=new ArrayList<PairValue>();
		   PairValue pv=new PairValue();
		   for(GsJudgeDocCollect g:list)
		   {
			  
			   pv=new PairValue();
			   pv.setKeyNo(g.getKeyNo());
			   if(gsBaseService.getByMd5(g.getKeyNo())!=null)
					   {
				   if(gsBaseService.getByMd5(g.getKeyNo()).getName()!=null&&!gsBaseService.getByMd5(g.getKeyNo()).getName().equals(""))
			       {
					   pv.setName(gsBaseService.getByMd5(g.getKeyNo()).getName());
					   pv.setCreditCode(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode());
					   log.info("Name not null="+g.getKeyNo()+":"+pv.getName());
			       }else
					   {
			    	   if(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode()!=null&&!gsBaseService.getByMd5(g.getKeyNo()).getCreditCode().equals(""))
			    	   {
			    		  // pv.setName(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode());
			    		  // log.info("Name is null and credit is not null="+pv.getName());
			    	   }else
			    	   {
			    		   continue;
			    	   }
			    	   }
					   }	   
			   keyNoList.add(pv); 
		   }
		   qknr.setCount(keyNoList.size());
		   log.info("size="+keyNoList.size());
		   qknr.setKeyNoList(keyNoList);
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}	
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" JudgeDocCollectKeyNoList 执行时间："+excTime+"s");
		return qknr;
	}
	@RequestMapping("/UpdateJudgeDocCollectKeyNo")
	public UpdateKeyNoResult UpdateJudgeDocCollectKeyNo(String keyNo,String data,String status) {
		log.info("===>UpdateJudgeDocCollectKeyNo="+keyNo);
		long startTime=System.currentTimeMillis();
		UpdateKeyNoResult qknr=new UpdateKeyNoResult();
		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsJudgeDocCollect gp=new GsJudgeDocCollect();
			gp.setKeyNo(keyNo);
			gp.setStatus(status);
			Date date= new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str = sdf.format(date);
			
			gp.setUpdateTime(str);
			gsJudgeDocCollectService.update(gp);
		 
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}
		
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" UpdateJudgeDocCollectKeyNo 执行时间："+excTime+"s");
		return qknr;
	}
	
}
